<?php

namespace app\api\controller;

use app\admin\model\Worktags;
use app\common\controller\Api;
use app\common\model\Addresses;
use think\Db;


/**
 * 地址
 */
class Address extends Api
{
    protected $noNeedLogin = [''];
    protected $noNeedRight = '*';
    protected $model = null;
    public function _initialize()
    {
        parent::_initialize();
        $this->model = new Addresses();
    }

    /*
     * 地址列表
     */
    public function address_list()
    {
        $params = $this->request->param();
        $query = $this->model->field('id,address,phone,name,is_default,sex,(select name from fa_area where fa_area.id=province_id) province,(select name from fa_area where fa_area.id=city_id) city,(select name from fa_area where fa_area.id=district_id) district')->where('user_id', '=', $this->auth->id);
        if (isset($params['is_default'])) {
            $query->where('is_default', '=', $params['is_default']);
        }

        $list = $query->select();
        foreach ($list as $item) {
            $item['address']=$item['province'].$item['city'].$item['district'].$item['address'];
        }
        $this->success('请求成功', $list);
    }

    public function default_list()
    {
        $params = $this->request->param();
        $query = $this->model->field('id,address,phone,name,is_default,sex,(select name from fa_area where fa_area.id=province_id) province,(select name from fa_area where fa_area.id=city_id) city,(select name from fa_area where fa_area.id=district_id) district')->where('user_id', '=', $this->auth->id);

        if(input('id')==''||input('id')==null){
            $query->where('is_default', '=', 1);
        }else{
            $query->where('id', '=', input('id'));
        }

        $list = $query->find();
        if($list){
            $list['address']=$list['province'].$list['city'].$list['district'].$list['address'];
        }
        $this->success('请求成功', $list);
    }
        /*
         * 添加修改地址
         */
    public function saveAddress()
    {
        Db::startTrans();
        $params = $this->request->param();
        if (empty($params['address']) || empty($params['name']) || empty($params['phone'])) {
            $this->error('缺少必需的参数');
        }
        if (!isValidPhoneNumber($params['phone'] )) {
            $this->error(__('手机号格式不正确'));
        }
        $addressData = [
            'address' => $params['address'],
            'name' => $params['name'],
            'phone' => $params['phone'],
            'is_default' => $params['is_default'],
            'sex' => $params['sex'],
            'user_id' =>  $this->auth->id,
            'province_id' => $params['province_id'],
            'city_id' => $params['city_id'],
            'district_id' => $params['district_id'],
        ];

        if (!empty($addressData['is_default'])) {
            $this->model->where('user_id', '=', $this->auth->id)
                ->where('is_default', '=', 1)
                ->update(['is_default' => 0]);
        }
        if ($params['id']!=""&&$params['id']!=null) {
            $address = $this->model->find($params['id']);
            if (!$address) {
                $this->error('地址不存在');
                db::rollback();
            }
            $this->model->where('id','=',$params['id'])->update($addressData);
            db::commit();
            $this->success('地址更新成功', []);
        } else {
            $this->model->insert($addressData);
            db::commit();
            $this->success('地址添加成功', []);

        }
    }


    public function address_info()
    {

        $params = $this->request->param();
        $info = $this->model->where('id','=',$params['id'])->find();
        $this->success('请求成功', $info);
    }

    public function save_default()
    {

        if (!$id = input('id/d')) {
            $this->error(__('请选择地址'));
        }
        if (!$is_default = input('is_default')) {
            $this->error(__('请选择状态'));
        }
        if ($is_default == 1) {
            $this->model->where('user_id', '=', $this->auth->id)
                ->where('id', '<>', $id)
                ->update(['is_default' => 0]);
        }
        $this->model->where('id', '=', $id)
            ->update(['is_default' => $is_default]);
        $this->success('修改成功', []);

    }


    public function del_address()
    {

        $params = $this->request->param();
        $info = $this->model->where('id','=',$params['id'])->delete();
        if($info){
            $this->success('请求成功', $info);
        }else{
            $this->error('删除失败');
        }

    }


}
